package org.keyyond.ms.business.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.keyyond.ms.business.common.CommonService;
import org.keyyond.ms.business.dao.MsBusinessMdmLogDao;
import org.keyyond.ms.business.model.MsBusinessMdmLog;
import org.keyyond.ms.business.service.MsBusinessMdmLogService;
import org.keyyond.ms.core.exception.FDFBexception;
import org.keyyond.ms.core.util.DateUtil;
import org.keyyond.ms.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MsBusinessMdmLogServiceImpl extends CommonService implements MsBusinessMdmLogService {

    @Autowired
    MsBusinessMdmLogDao msBusinessMdmLogDao;

    @Override
    public int saveReturnPid(String param) {
	try {
	    if (StringUtils.isEmpty(param) || param.equals("{}"))
		return -1;
	    MsBusinessMdmLog bLog = new MsBusinessMdmLog();
	    bLog = JsonUtil.readJson2Bean(param, MsBusinessMdmLog.class);
	    if (bLog == null)
		throw new FDFBexception("将传入的Json字符串转换成Bean失败");
	    MsBusinessMdmLog oldBLog = getBankLogById(param);
	    if (null != oldBLog) {
		oldBLog.setResultInfo(bLog.getResultInfo());
		msBusinessMdmLogDao.update(oldBLog);
		return oldBLog.getpId();
	    } else {
		bLog.setCreateDate(DateUtil.formatDateTime(new Date()));
		return msBusinessMdmLogDao.saveReturnPid(bLog);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return -1;
	}
    }

    @Override
    public MsBusinessMdmLog getBankLogById(String param) {
	MsBusinessMdmLog vlog = new MsBusinessMdmLog();
	try {
	    if (StringUtils.isEmpty(param) || param.equals("{}"))
		return null;
	    MsBusinessMdmLog bLog = new MsBusinessMdmLog();
	    bLog = JsonUtil.readJson2Bean(param, MsBusinessMdmLog.class);
	    if (bLog == null)
		throw new FDFBexception("将传入的Json字符串转换成Bean失败");
	    String ifCode = bLog.getIfCode();
	    String ipName = bLog.getIpName();
	    String id = bLog.getId();
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("ifcode", ifCode);
	    map.put("ipname", ipName);
	    map.put("id", id);
	    List<MsBusinessMdmLog> vlogList = msBusinessMdmLogDao.getBankLogById(map);
	    if (vlogList.size() >= 1) {
		vlog = vlogList.get(0);
	    }
	    return vlog;
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return null;
    }

}
